

;; make command
(require 'compile)
(add-hook 'c-mode-hook
          (lambda ()
            (unless (file-exists-p "Makefile")
              (set (make-local-variable 'compile-command)
                   ;; emulate make's .c.o implicit pattern rule, but with
                   ;; different defaults for the CC, CPPFLAGS, and CFLAGS
                   ;; variables:
                   ;; $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
                   (let ((file (file-name-nondirectory buffer-file-name)))
                     (format "%s -g -o %s  %s && ./%s"
                             (or (getenv "CC") "gcc")
                             (file-name-sans-extension file)
                             ;; (or (getenv "CPPFLAGS") "-DDEBUG=9")
                             ;; (or (getenv "CFLAGS") "-ansi -pedantic -Wall -g")
                             file
                             (file-name-sans-extension file)
                             ))))))

(add-hook 'c++-mode-hook
          (lambda ()
            (unless (file-exists-p "Makefile")
              (set (make-local-variable 'compile-command)
                   ;; emulate make's .c.o implicit pattern rule, but with
                   ;; different defaults for the CC, CPPFLAGS, and CFLAGS
                   ;; variables:
                   ;; $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
                   (let ((file (file-name-nondirectory buffer-file-name)))
                     (format "%s -g -o %s  %s && ./%s"
                             (or (getenv "CC") "g++")
                             (file-name-sans-extension file)
                             ;; (or (getenv "CPPFLAGS") "-DDEBUG=9")
                             ;; (or (getenv "CFLAGS") "-ansi -pedantic -Wall -g")
                             file
                             (file-name-sans-extension file)
                             ))))))

;; change the gud-gdb command name
(add-hook 'c++-mode-hook
          (lambda ()
            (set (make-local-variable 'gud-gdb-command-name)
                 (let ((file (file-name-nondirectory buffer-file-name)))
                   (format "gdb -q  %s"
                           (file-name-sans-extension file))))))

(add-hook 'c++-mode-hook
          (lambda ()
            (set (make-local-variable 'gud-gud-gdb-command-name)
                 (let ((file (file-name-nondirectory buffer-file-name)))
                   (format "gdb -q  %s"
                           (file-name-sans-extension file))))))




;; gdb shortcut
(spacemacs/declare-prefix-for-mode 'c++-mode "md" "debug")
(spacemacs/set-leader-keys-for-major-mode 'c++-mode
  "dd" 'gdb
  "db" 'gud-break
  "dB" 'gud-tbreak
  "dr" 'gud-remove)



;; (spacemacs/declare-prefix-for-mode 'gud-mode "mf" "frame")
(spacemacs/set-leader-keys-for-major-mode 'gud-mode
  "m" 'gdb-many-windows
  "i" 'gdb-frame-io-buffer
  "b" 'gdb-frame-breakpoints-buffer
  "d" 'gdb-frame-gdb-buffer
  "s" 'gdb-frame-stack-buffer
  "l" 'gdb-frame-locals-buffer)

(spacemacs/set-leader-keys-for-major-mode 'gdb-inferior-io-mode
  "m" 'gdb-many-windows
  "d" 'gdb-frame-gdb-buffer)
(spacemacs/set-leader-keys-for-major-mode 'gdb-locals-mode
  "m" 'gdb-many-windows
  "d"       'gdb-frame-gdb-buffer)
(spacemacs/set-leader-keys-for-major-mode 'gdb-frames-mode
  "m" 'gdb-many-windows
  "d" 'gdb-frame-gdb-buffer)
(spacemacs/set-leader-keys-for-major-mode 'gdb-breakpoints-mode
  "m" 'gdb-many-windows
  "d" 'gdb-frame-gdb-buffer)


(add-hook 'gud-mode-hook
          (lambda ()
            (evil-force-normal-state)))

(add-hook 'gdb-locals-mode-hook
          (lambda ()
            (evil-force-normal-state)))

(add-hook 'gdb-frames-mode-hook
          (lambda ()
            (evil-force-normal-state)))

(add-hook 'gdb-inferior-io-mode-hook
          (lambda ()
            (evil-force-normal-state)))

(add-hook 'gdb-breakpoints-mode-hook
          (lambda ()
            (evil-force-normal-state)))
